package lib.quasar.widget.menu;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import lib.quasar.util.DimenUtil;
import lib.quasar.widget.R;
import lib.quasar.widget.paint.PaintManager;

/**
 * description: 顶部, 导航菜单
 * created by kalu on 2018/5/15 9:29
 */
public final class NavigationView extends View {

    private boolean isPressBack, isPressMenu, isPressMenu2;

    private boolean hasBack, hasMenu1, hasMenu2;

    private final float mArrowLeft = DimenUtil.dp2px(21f);
    private final float mArrowRight = DimenUtil.dp2px(30f);
    private final float mArrowHeight = DimenUtil.dp2px(8f);

    private String mBackText;
    private float mBackWidth = DimenUtil.dp2px(60f);
    private float mBackSize = 10f;
    private int mBackColor = Color.WHITE;

    private String mMenu1Text;
    private float mMenu1Width = DimenUtil.dp2px(60f);
    private float mMenu1Size = 10f;
    private int mMenu1Color = Color.WHITE;

    private String mMenu2Text;
    private float mMenu2Width = DimenUtil.dp2px(60f);
    private float mMenu2Size = 10f;
    private int mMenu2Color = Color.WHITE;

    private String mTextTitle;
    private float mTextSize = 10f;
    private int mTextColor = Color.BLACK;
    private int mTextBackground = Color.WHITE;

    public NavigationView(Context context) {
        this(context, null, 0);
    }

    public NavigationView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NavigationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typed = null;
        try {
            typed = context.obtainStyledAttributes(attrs, R.styleable.NavigationView, 0, 0);
            hasBack = typed.getBoolean(R.styleable.NavigationView_nv_has_back, true);
            hasMenu1 = typed.getBoolean(R.styleable.NavigationView_nv_has_menu1, false);
            hasMenu2 = typed.getBoolean(R.styleable.NavigationView_nv_has_menu2, false);

            mBackWidth = typed.getDimension(R.styleable.NavigationView_nv_back_width, mBackWidth);
            mMenu1Width = typed.getDimension(R.styleable.NavigationView_nv_menu1_width, mMenu1Width);
            mMenu2Width = typed.getDimension(R.styleable.NavigationView_nv_menu2_width, mMenu2Width);

            mBackText = typed.getString(R.styleable.NavigationView_nv_back_text);
            mBackSize = typed.getDimension(R.styleable.NavigationView_nv_back_size, mBackSize);
            mBackColor = typed.getColor(R.styleable.NavigationView_nv_back_color, mBackColor);

            mMenu1Text = typed.getString(R.styleable.NavigationView_nv_menu1_text);
            mMenu1Size = typed.getDimension(R.styleable.NavigationView_nv_menu1_size, mMenu1Size);
            mMenu1Color = typed.getColor(R.styleable.NavigationView_nv_menu1_color, mMenu1Color);

            mMenu2Text = typed.getString(R.styleable.NavigationView_nv_menu2_text);
            mMenu2Size = typed.getDimension(R.styleable.NavigationView_nv_menu2_size, mMenu2Size);
            mMenu2Color = typed.getColor(R.styleable.NavigationView_nv_menu2_color, mMenu2Color);

            mTextTitle = typed.getString(R.styleable.NavigationView_nv_title_text);
            mTextSize = typed.getDimension(R.styleable.NavigationView_nv_title_size, mTextSize);
            mTextColor = typed.getColor(R.styleable.NavigationView_nv_title_color, mTextColor);
            mTextBackground = typed.getColor(R.styleable.NavigationView_nv_title_background, mTextBackground);
        } catch (Exception e) {
        } finally {
            if (null == typed) return;
            typed.recycle();
        }
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        final int bottom = getHeight();
        final int right = getWidth();
        canvas.drawRect(0f, 0f, right, bottom, PaintManager.getTextPaint(mTextBackground));

        if (hasBack) {

            if (isPressBack) {
                final int color1 = getContext().getResources().getColor(R.color.color_bg_press);
                canvas.drawRect(0f, 0f, mBackWidth, bottom, PaintManager.getTextPaint(color1));
            }

            if (TextUtils.isEmpty(mBackText)) {
                final Paint paint1 = PaintManager.getLinePaint(mBackColor, PaintManager.LiNE_ARROW);
                final Path path = new Path();
                path.moveTo(mArrowRight, bottom * 0.5f - mArrowHeight);
                path.lineTo(mArrowLeft, bottom * 0.5f);
                path.lineTo(mArrowRight, bottom * 0.5f + mArrowHeight);
                canvas.drawPath(path, paint1);
            } else {
                final Paint paint = PaintManager.getTextPaint(mBackColor, mBackSize);
                final Paint.FontMetrics fontMetrics = paint.getFontMetrics();
                final float font = (fontMetrics.bottom - fontMetrics.top) * 0.3f;
                canvas.drawText(mBackText, mBackWidth * 0.5f, bottom * 0.5f + font, paint);
            }
        }

        if (hasMenu1) {

            if (isPressMenu) {
                final int color1 = getContext().getResources().getColor(R.color.color_bg_press);
                canvas.drawRect(getWidth() - mMenu1Width, 0f, getWidth(), bottom, PaintManager.getTextPaint(color1));
            }

            if (!TextUtils.isEmpty(mMenu1Text)) {
                final Paint paint2 = PaintManager.getTextPaint(mMenu1Color, mMenu1Size);
                final Paint.FontMetrics fontMetrics2 = paint2.getFontMetrics();
                final float font2 = (fontMetrics2.bottom - fontMetrics2.top) * 0.3f;
                canvas.drawText(mMenu1Text, getWidth() - mMenu1Width * 0.5f, bottom * 0.5f + font2, paint2);
            }
        }

        if (hasMenu2) {

            if (isPressMenu2) {
                final int color1 = getContext().getResources().getColor(R.color.color_bg_press);
                canvas.drawRect(getWidth() - mMenu1Width - mMenu2Width, 0f, getWidth() - mMenu1Width, bottom, PaintManager.getTextPaint(color1));
            }

            if (!TextUtils.isEmpty(mMenu2Text)) {
                final Paint paint2 = PaintManager.getTextPaint(mMenu2Color, mMenu2Size);
                final Paint.FontMetrics fontMetrics2 = paint2.getFontMetrics();
                final float font2 = (fontMetrics2.bottom - fontMetrics2.top) * 0.3f;
                canvas.drawText(mMenu2Text, getWidth() - mMenu2Width * 0.5f - mMenu1Width, bottom * 0.5f + font2, paint2);
            }
        }


        if (!TextUtils.isEmpty(mTextTitle)) {
            final Paint paint3 = PaintManager.getTextPaint(mTextColor, mTextSize);
            final Paint.FontMetrics fontMetrics3 = paint3.getFontMetrics();
            final float font3 = (fontMetrics3.bottom - fontMetrics3.top) * 0.3f;
            canvas.drawText(mTextTitle, getWidth() * 0.5f, bottom * 0.5f + font3, paint3);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        final boolean ok1 = hasBack && event.getX() <= mBackWidth;
        final boolean ok2 = hasMenu1 && event.getX() >= (getWidth() - mMenu1Width);
        final boolean ok4 = hasMenu2 && event.getX() >= (getWidth() - mMenu1Width - mMenu2Width) && event.getX() <= (getWidth() - mMenu1Width);
        final boolean ok3 = (ok1 || ok2 || ok4) && event.getY() >= getHeight();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (ok1) {
                    isPressBack = true;
                    postInvalidate();
                    return true;
                } else if (ok2) {
                    isPressMenu = true;
                    postInvalidate();
                    return true;
                } else if (ok4) {
                    isPressMenu2 = true;
                    postInvalidate();
                    return true;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (!ok3) break;
                isPressMenu = false;
                isPressBack = false;
                isPressMenu2 = false;
                postInvalidate();
                return super.onTouchEvent(event);
            case MotionEvent.ACTION_OUTSIDE:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                if (!isPressMenu && !isPressBack && !isPressMenu2) break;
                if (null != listener) {
                    if (ok1) listener.onMenu(true, false);
                    else if (ok2) listener.onMenu(false, true);
                }

                if (null != listener4) {
                    if (ok1) listener4.onMenu(true, false, false);
                    else if (ok2) listener4.onMenu(false, true, false);
                    else if (ok4) listener4.onMenu(false, false, true);
                }
                if (null != listener2 && isPressMenu) {
                    listener2.onMenu();
                }

                if (null != listener3 && isPressBack) {
                    listener3.onBack();
                }

                isPressMenu = false;
                isPressMenu2 = false;
                isPressBack = false;
                postInvalidate();
                break;
        }

        return super.onTouchEvent(event);
    }

    public void setMenuEnable(boolean enable) {
        this.hasMenu1 = enable;
        postInvalidate();
    }

    public void setTitle(String str) {
        if (TextUtils.isEmpty(str)) return;
        this.mTextTitle = str;
        postInvalidate();
    }

    /**************************************************************************/

    private OnNavigation2ChangeListener listener4;

    public void setOnNavigation2ChangeListener(OnNavigation2ChangeListener listener) {
        this.listener4 = listener;
    }

    public interface OnNavigation2ChangeListener {

        void onMenu(boolean isBack, boolean isMenu1, boolean isMenu2);
    }

    /**************************************************************************/

    private OnNavigationChangeListener listener;

    public void setOnNavigationChangeListener(OnNavigationChangeListener listener) {
        this.listener = listener;
    }

    public interface OnNavigationChangeListener {

        void onMenu(boolean isBack, boolean isMenu);
    }

    /**************************************************************************/

    private OnMenuChangeListener listener2;

    public void setOnMenuChangeListener(OnMenuChangeListener listener) {
        this.listener2 = listener;
    }

    public interface OnMenuChangeListener {
        void onMenu();
    }

    /**************************************************************************/

    private OnBackChangeListener listener3;

    public void setOnBackChangeListener(OnBackChangeListener listener) {
        this.listener3 = listener;
    }

    public interface OnBackChangeListener {
        void onBack();
    }
}
